package leetcode.D100.T15;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class Solution {
    // 一刷，太麻烦
    /*public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if(nums.length < 3) {
            return result;
        }
        Arrays.sort(nums);
        for(int i=0; i<nums.length-2; ++i) {
            if(nums[i] > 0) {
                break;
            }
            if(i > 0 && nums[i] == nums[i-1]) {
                continue;
            }
            search(result, nums, i);
        }
        return result;
    }
    private void search(List<List<Integer>> result, int[] nums, int start) {
        int target = -nums[start];
        int left = start + 1, right = nums.length-1;
        while(left < right) {
            int sum = nums[left] + nums[right];
            if(sum == target) {
                List<Integer> ans = new ArrayList<>(3);
                ans.add(nums[start]);
                ans.add(nums[left]);
                ans.add(nums[right]);
                result.add(ans);
                left++;
                right--;
                while(left < right) {
                    if(nums[left] == nums[left-1]) {
                        left++;
                    } else {
                        break;
                    }
                    if(nums[right] == nums[right+1]) {
                        right--;
                    } else {
                        break;
                    }
                }
            } else if(sum < target) {
                left++;
            } else {
                right--;
            }
        }
    }*/

    // 二刷
    /*public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> results = new ArrayList<>();
        Arrays.sort(nums);
        for (int i = 0; i < nums.length-2; i++) {
            while (i > 0 && i<nums.length-2 && nums[i] == nums[i-1])
                ++i;
            List<List<Integer>> lists = towSum(nums, i + 1, -nums[i]);
            for (List<Integer> list : lists) {
                list.add(nums[i]);
                results.add(list);
            }
        }
        return results;
    }

    private List<List<Integer>> towSum(int[] nums, int start, int target) {
        List<List<Integer>> results = new ArrayList<>();
        int left = start, right = nums.length-1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            int baseLeft = nums[left], baseRight = nums[right];
            if (sum < target) {
                while (left < right && nums[left] == baseLeft)
                    left++;
            } else if (sum > target) {
                while (left < right && nums[right] == baseRight)
                    right--;
            } else {
                List<Integer> result = new ArrayList<>();
                result.add(nums[left]); result.add(nums[right]);
                results.add(result);
                while (left < right && nums[left] == baseLeft)
                    left++;
                while (left < right && nums[right] == baseRight)
                    right--;
            }
        }
        return results;
    }*/

    // 三刷
    public List<List<Integer>> threeSum(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        if (nums.length < 3)
            return result;
        Arrays.sort(nums);
        for (int i=0; i<nums.length; ++i) {
            if (i > 0 && nums[i] == nums[i-1])
                continue;
            List<List<Integer>> temp = twoSum(nums, i + 1, -nums[i]);
            result.addAll(temp);
        }
        return result;
    }

    private List<List<Integer>> twoSum(int[] nums, int start, int target) {
        List<List<Integer>> result = new ArrayList<>();
        if (start > nums.length-2)
            return result;
        int left = start, right = nums.length-1;
        while (left < right) {
            int sum = nums[left] + nums[right];
            int leftBase = nums[left], rightBase = nums[right];
            if (sum == target) {
                List<Integer> temp = new ArrayList<>();
                temp.add(-target);
                temp.add(nums[left]);
                temp.add(nums[right]);
                result.add(temp);
                while (left < right && nums[left] == leftBase)
                    left++;
                while (left < right && nums[right] == rightBase)
                    right--;
            } else if (sum < target) {
                while (left < right && nums[left] == leftBase)
                    left++;
            } else {
                while (left < right && nums[right] == rightBase)
                    right--;
            }
        }
        return result;
    }
}